/*
 * @(#)GSProductInfoDownServlet.java   
 *
 * Project: CUPSecureBM
 * 
 * Modify Information:
 *  Author          Date                Description
 * ==============   =================   =====================
 *  yonggangguo     2005-12-29             Create this file
 *  Tuthmos Liu     2008-06-16           增加图件号
 * Copyright notice:
 *   Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd.
 *   1808 Room, Science & Technology Building, No. 9 South Street,
 *   Zhong Guan Cun, Haidian District, Beijing ,100081, China
 *   All rights reserved.
 *
 *   This software is the confidential and proprietary information of
 *   HiTRUST.COM, Inc. ('Confidential Information').  You shall not
 *   disclose such Confidential Information and shall use it only in
 *   accordance with the terms of the license agreement you entered
 *   into with HiTRUST.
 */

package com.hitrust.gs.action;

import java.io.OutputStream;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.lib.GSLogWriter;

import framework.core.DBConnection;

public class GSBusOrderDownServlet extends HttpServlet {

    public void init(ServletConfig aConfig) throws ServletException {
        super.init(aConfig);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    	   // 得到所有传入的参数(订单明细号，以逗号隔开) 
        String obj = request.getParameter("params");
    	
    	response.setContentType("application/excel;charset=UTF-16");
        

        response.setHeader("Content-disposition", "attachment; filename=busOrderInfo.xls");
        response.setHeader("Content-Description", "订货要约书信息");

       
        DBConnection dbcon = null;
        OutputStream tOutputStream = null;
        try {
            dbcon = DBConnection.open();
            tOutputStream = response.getOutputStream();
            GSOrderProcessor tGSOrderProc = new GSOrderProcessor();
            ArrayList tOrderDetailList = tGSOrderProc.getOrderDetailForSignNew(obj);
            
       
             writeExcel(tOrderDetailList, tOutputStream);

       
            
        } catch (Exception e) {
            GSLogWriter.error(e.getMessage());
        } finally {
            try {
                if (tOutputStream != null) {
                    tOutputStream.close();
                }
                dbcon.close();
            } catch (Exception e) {
            }
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        doPost(request, response);
    }

    private void writeExcel(ArrayList aList, OutputStream tOutputStream) {
    	String orderid="";
        DBConnection con = null;
        try {	
        		GSOrderBean orderBean=null;
        		if(aList!=null&&aList.size()>0){
        			GSOrderDAO orderDao=new GSOrderDAO();
        			String[] strs=(String[])aList.get(0);
        			orderid=strs[20];
        			orderBean=orderDao.selectOrderByCode(orderid);
        		}
        	
                con = DBConnection.open();
                HSSFWorkbook tWb = new HSSFWorkbook();
                HSSFSheet tSheet = tWb.createSheet();
                tSheet.setColumnWidth((short) 0, (short) (28 * 256));
                tSheet.setColumnWidth((short) 1, (short) (40 * 256));
                tSheet.setColumnWidth((short) 2, (short) (28 * 256));
                tSheet.setColumnWidth((short) 3, (short) (28 * 256));
                tSheet.setColumnWidth((short) 4, (short) (28 * 256));
                tSheet.setColumnWidth((short) 5, (short) (28 * 256));
                String[] tInitOrder = (String[])aList.get(0);
                tWb.setSheetName(0, "订货要约书", HSSFCell.ENCODING_UTF_16);
                HSSFRow tRow = tSheet.createRow((short) 0);
                createLeftCell(tWb, tRow, (short) 0, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow, (short) 1, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow, (short) 2, "上海巴士物资实业有限公司", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow, (short) 3, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow, (short) 4, "", HSSFCellStyle.ALIGN_CENTER,false);
                createRightCell(tWb, tRow, (short) 5, "", HSSFCellStyle.ALIGN_CENTER,false);
                HSSFRow tRow1 = tSheet.createRow((short) 1);
                createLeftCell(tWb, tRow1, (short) 0, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow1, (short) 1, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow1, (short) 2, "沃尔沃配件订货要约书", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow1, (short) 3, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow1, (short) 4, "", HSSFCellStyle.ALIGN_CENTER,false);
                createRightCell(tWb, tRow1, (short) 5, "", HSSFCellStyle.ALIGN_CENTER,false);
                HSSFRow tRow2 = tSheet.createRow((short) 2);
                createCell(tWb, tRow2, (short) 0, "订单号： ", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow2, (short) 1, orderid , HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow2, (short) 2, "级别：", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow2, (short) 3, "1级", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow2, (short) 4, "订单日期  ", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow2, (short) 5, orderBean==null?"":orderBean.getGs_order_create_date().substring(0,10), HSSFCellStyle.ALIGN_CENTER,true);
                
                HSSFRow tRow3 = tSheet.createRow((short) 3);
                createCell(tWb, tRow3, (short) 0, "需方", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow3, (short) 1, "上海巴士物资采购管理有限公司", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow3, (short) 2, "地址", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow3, (short) 3, "汶水路140号 204室", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow3, (short) 4, "", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow3, (short) 5, "", HSSFCellStyle.ALIGN_CENTER,true);
                HSSFRow tRow5 = tSheet.createRow((short) 4);
                createCell(tWb, tRow5, (short) 0, "联系电话", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow5, (short) 1, "33608584*251", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow5, (short) 2, "传 真", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow5, (short) 3, "56776713", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow5, (short) 4, "联系人", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow5, (short) 5, "徐秉钦", HSSFCellStyle.ALIGN_CENTER,true);
                HSSFRow tRow6 = tSheet.createRow((short) 5);
                createCell(tWb, tRow6, (short) 0, "供方", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow6, (short) 1, "上海申沃客车有限公司售后服务部、配件部",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow6, (short) 2, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow6, (short) 3, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow6, (short) 4, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow6, (short) 5, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                
                HSSFRow tRow7 = tSheet.createRow((short) 6);
                createCell(tWb, tRow7, (short) 0, "地址", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow7, (short) 1, "上海市闵行区光中路18号",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow7, (short) 2, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow7, (short) 3, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow7, (short) 4, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow7, (short) 5, "",
                		HSSFCellStyle.ALIGN_CENTER,true);
                HSSFRow tRow8 = tSheet.createRow((short) 7);
                createCell(tWb, tRow8, (short) 0, "联系电话", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow8, (short) 1, "24160089",
                		HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow8, (short) 2, "传真",	HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow8, (short) 3, "24160292", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow8, (short) 4, "联系人", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow8, (short) 5, "童丽娜", HSSFCellStyle.ALIGN_CENTER,true);
                HSSFRow tRow9 = tSheet.createRow((short) 8);
                createCell(tWb, tRow9, (short) 0, "收货仓库", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow9, (short) 1, tInitOrder[21],
                		HSSFCellStyle.ALIGN_CENTER,false);
                createCell(tWb, tRow9, (short) 2, "电话",	HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow9, (short) 3, tInitOrder[23], HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow9, (short) 4, "地 址：", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow9, (short) 5, tInitOrder[19], HSSFCellStyle.ALIGN_CENTER,true);
                HSSFRow tRow99 = tSheet.createRow((short) 9);
                createCell(tWb, tRow99, (short) 0, "收货确认", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow99, (short) 1, "", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow99, (short) 2, "", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow99, (short) 3, "", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow99, (short) 4, "", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow99, (short) 5, "", HSSFCellStyle.ALIGN_CENTER,true);
                HSSFRow tRow10 = tSheet.createRow((short) 10);
                createLeftCell(tWb, tRow10, (short) 0, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow10, (short) 1, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow10, (short) 2, "需求产品信息", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow10, (short) 3, "", HSSFCellStyle.ALIGN_CENTER,false);
                createUpDownCell(tWb, tRow10, (short) 4, "", HSSFCellStyle.ALIGN_CENTER,false);
                createRightCell(tWb, tRow10, (short) 5, "", HSSFCellStyle.ALIGN_CENTER,false);
                HSSFRow tRow11 = tSheet.createRow((short) 11);
                createCell(tWb, tRow11, (short) 0, "订单明细编号", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow11, (short) 1, "图件号", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow11, (short) 2, "名称", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow11, (short) 3, "网号", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow11, (short) 4, "数量", HSSFCellStyle.ALIGN_CENTER,true);
                createCell(tWb, tRow11, (short) 5, "备注", HSSFCellStyle.ALIGN_CENTER,true);
             
                for (int i = 0; i < aList.size(); i++) {
                    tRow = tSheet.createRow((short) (i + 12));
                    String[] obj = (String[]) aList.get(i);
                    createCell(tWb, tRow, (short) 0, obj[0], HSSFCellStyle.ALIGN_CENTER,true);
                    createCell(tWb, tRow, (short) 1, obj[18], HSSFCellStyle.ALIGN_CENTER,true);
                    createCell(tWb, tRow, (short) 2, obj[2], HSSFCellStyle.ALIGN_CENTER,true);
                    createCell(tWb, tRow, (short) 3, obj[1], HSSFCellStyle.ALIGN_CENTER,true);
                    createCell(tWb, tRow, (short) 4, obj[7]+obj[8], HSSFCellStyle.ALIGN_CENTER,true);
                    createCell(tWb, tRow, (short) 5, "", HSSFCellStyle.ALIGN_CENTER,true);
               }
                tWb.write(tOutputStream);
                tOutputStream.flush();
      
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception e) {
            }
        }
    }
    
	private HSSFCell createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue, short aAlign, boolean border) {
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellValue(aValue);
		HSSFCellStyle tCellStyle = aWb.createCellStyle();
		tCellStyle.setAlignment(aAlign);
		tCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		if (border) {
			tCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setTopBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		}
		tCell.setCellStyle(tCellStyle);
		return tCell;
	}
	
	private HSSFCell createLeftCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue, short aAlign, boolean border) {
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellValue(aValue);
		HSSFCellStyle tCellStyle = aWb.createCellStyle();
		tCellStyle.setAlignment(aAlign);
		tCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		if (border) {
			tCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setTopBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		}
		tCell.setCellStyle(tCellStyle);
		return tCell;
	}
	
	private HSSFCell createRightCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue, short aAlign, boolean border) {
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellValue(aValue);
		HSSFCellStyle tCellStyle = aWb.createCellStyle();
		tCellStyle.setAlignment(aAlign);
		tCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		if (border) {
			tCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setTopBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		}
		tCell.setCellStyle(tCellStyle);
		return tCell;
	}
	
	private HSSFCell createUpDownCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue, short aAlign, boolean border) {
		HSSFCell tCell = aRow.createCell(column);
		tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
		tCell.setCellValue(aValue);
		HSSFCellStyle tCellStyle = aWb.createCellStyle();
		tCellStyle.setAlignment(aAlign);
		tCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		if (border) {
			tCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
			tCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setTopBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
			tCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		}
		tCell.setCellStyle(tCellStyle);
		return tCell;
	}

    private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue) {
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
    }

    private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue, short aAlign) {
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
        HSSFCellStyle tCellStyle = aWb.createCellStyle();
        tCellStyle.setAlignment(aAlign);
        tCell.setCellStyle(tCellStyle);
    }
}
